package main.java.DMDE.controllers;

import java.util.ArrayList;
import java.util.List;

import main.java.DMDE.controllers.factories.IParameterControllerFactory;
import main.java.DMDE.infrastructure.IProjectContext;
import main.java.DMDE.model.Attribute;
import main.java.DMDE.model.Method;
import main.java.DMDE.model.Modifier;
import main.java.DMDE.model.Parameter;
import main.java.DMDE.model.Type;
import main.java.DMDE.model.Visibility;
import main.java.DMDE.view.IOperationView;

public class OperationController extends BaseController implements
	IOperationController {
    
    private IOperationView operationView;
    private List<Method> operations;
    private IParameterControllerFactory paramControllerFactory;
    private IParameterController paramController;

    public OperationController(IProjectContext projectContext,
	    	IParameterControllerFactory paramControllerFactory,IOperationView operationView) {
	super(projectContext);
	this.operations = new ArrayList<Method>();
	this.operationView = operationView;
	this.operationView.setController(this);
	this.paramControllerFactory = paramControllerFactory;
    }

    @Override
    public IOperationView getOperationView() {
	return operationView;
    }
    
    @Override
    public void addParameter(Method operatSelected) {
	this.paramController = this.paramControllerFactory.create();
	if(operatSelected != null)
    		operationView.setParameters(paramController.getParameterView(),operatSelected.getParameters());
	operationView.addParameterView(paramController.getParameterView());	
    }
    
    @Override
    public void editParameter(Method operatSelected) {
	this.paramController = this.paramControllerFactory.create();
	if(operatSelected != null){
        	operationView.setParameters(paramController.getParameterView(),operatSelected.getParameters());
        	operationView.addParameterView(paramController.getParameterView());
	}	
    }
    
    @Override
    public Method addOperation() {
	String operatName = operationView.getName();
	Type operatType = operationView.getReturnType();
	Visibility operatVisibility = operationView.getVisibility();
	List<Modifier> modifiers = operationView.getModifiers();
	Method operat = new Method(operatName);
	operat.setReturnType(operatType);
	operat.setVisibility(operatVisibility);
	if(!modifiers.isEmpty())
	    operat.setModifiers(modifiers);
	if(this.paramController != null)
	    operat.addParameters(this.paramController.getParameters());
	this.operations.add(operat);
	return operat;
    }
    
    @Override
    public void setOperations(List<Method> operations) {
	this.operations = operations;
	this.operationView.setOperations(operations);
    }
        
    @Override
    public boolean editOperation(Method operation) {
	String operatName = operationView.getName();
	if (operatName.equals("")) 
	    return false;
	operation.setName(operationView.getName());
	operation.setReturnType(operationView.getReturnType());
	operation.setVisibility(operationView.getVisibility());
	if(!operationView.getModifiers().isEmpty())
	    operation.setModifiers(operationView.getModifiers());
	return true;
    }

    @Override
    public boolean removeOperation(Method operatSelected) {
	 return operations.remove(operatSelected);
    }
    
    @Override
    public List<Method> getMethods() {
	return operationView.getOperations();
    }
}
